package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: atguigu
 * @create: 2025-03-19 14:03
 */
@Slf4j
@RestController
@RequestMapping("/api/album/test")
public class TestController {

    /**
     * SpringCache 将方法上缓存注解参数作为key 方法结果作为Value
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Cacheable(value = "userCache", key = "#id")
    public String queryCache(@PathVariable("id") Long id) {
        log.info("根据ID查询数据获取业务数据");
        return "id数据：" + id;
    }

    /**
     * 情况缓存
     *
     * @param id
     */
    @CacheEvict(value = "userCache", key = "#id")
    @GetMapping("/update/{id}")
    public void updateUser(@PathVariable("id") Long id) {
        log.info("根据ID更新取业务数据");
    }

    @Autowired
    private TestService testService;

    @GetMapping("/read/{id}")
    public String read(@PathVariable long id) {
        String result = testService.read(id);
        return "读数据成功：" + result;
    }


    @GetMapping("/write/{id}")
    public String write(@PathVariable long id) {
        testService.write(id);
        return "写数据成功";
    }


}
